﻿using System.Windows;
using GalaSoft.MvvmLight.Messaging;
using System.Windows.Threading;
using System;
using ASoft.WPF.ViewModel;
using GalaSoft.MvvmLight.Threading;

namespace ASoft.WPF.View
{
    /// <summary>
    /// Description for MainView.
    /// </summary>
    public partial class MainView : Window
    {
        DispatcherTimer dispatcherTimer;
        /// <summary>
        /// Initializes a new instance of the MainView class.
        /// </summary>
        public MainView()
        {
            InitializeComponent();
            MessengerRegister();
            InitDispatcherTimer();
        }

        private void InitDispatcherTimer()
        {
            dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
            dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
            dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
            dispatcherTimer.Start();
        }

        private void MessengerRegister()
        {
            Messenger.Default.Register<MessageBase>(this, "TopOpenParams", msg =>
               {
                   PLCView view = new PLCView();
                   view.ShowDialog();
               });

            Messenger.Default.Register<MessageBase>(this, "TopWeatherStation", msg =>
               {
                   WeatherStationView view = new WeatherStationView();
                   view.ShowDialog();
               });

            Messenger.Default.Register<MessageBase>(this, "TopShutdown", msg =>
            {
                Application.Current.Shutdown();
            });

            Messenger.Default.Register<MessageBase>(this, "RoomFullScreen", msg =>
               {
                   RoomAllPositionView view = new RoomAllPositionView();
                   view.Show();
               });


            Messenger.Default.Register<MessageBase>(this, "TopHistory", msg =>
               {
                   HistoryView view = new HistoryView();
                   view.ShowDialog();
               });

            Messenger.Default.Register<MessageBase>(this, "Users", msg =>
               {
                   UsersView view = new UsersView();
                   view.ShowDialog();
               });

            Messenger.Default.Register<MessageBase>(this, "DbLog", msg =>
             {
                 DbLogView view = new DbLogView();
                 view.ShowDialog();
             });

            Messenger.Default.Register<MessageBase>(this, "NotImp", msg =>
            {
                MessageBox.Show("未实现");
            });

            Messenger.Default.Register<Exception>(this, "taskException", msg =>
                {
                    if (msg == null)
                    {
                        return;
                    }
                    DispatcherHelper.UIDispatcher.BeginInvoke
                    (DispatcherPriority.Normal, new System.Threading.ParameterizedThreadStart((e) =>
                    {
                        var vm = this.DataContext as MainViewModel;
                        if (vm == null)
                        {
                            return;
                        }
                        vm.ErrorText = msg.Message + DateTime.Now.ToString("HH:mm:ss");
                    }), msg);

                });
        }
        private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            var vm = this.DataContext as MainViewModel;
            if (vm != null)
            {
                vm.RaiseStatusTextChanged();
            }
        }
    }
}